/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pranavpandey.ohos.dynamic.toasts.sample.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import ohos.global.resource.NotExistException;
import ohos.media.image.PixelMap;
import ohos.utils.IntentConstants;
import ohos.utils.net.Uri;

import com.pranavpandey.ohos.dynamic.toasts.DynamicHint;
import com.pranavpandey.ohos.dynamic.toasts.DynamicToast;
import com.pranavpandey.ohos.dynamic.toasts.sample.CustomDialog;
import com.pranavpandey.ohos.dynamic.toasts.sample.ImageTool;
import com.pranavpandey.ohos.dynamic.toasts.sample.ResourceTable;
import com.pranavpandey.ohos.dynamic.toasts.watcher.DynamicColorUtils;
import com.pranavpandey.ohos.dynamic.toasts.watcher.DynamicUnitUtils;

import java.io.IOException;

/**
 * ToastDialog 实例代码
 *
 * @since 2021-07-13
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    Button warningIconBtn;
    Button warningBtn;
    Button toastDefBtn;
    Button toastDefIconBtn;
    Button toastErrorBtn;
    Button toastSuccessBtn;
    Button toastWarningBtn;
    Button toastCusBtn;
    Button toastCusIconBtn;
    Button toastErrorColorBtn;
    Button toastSuccessColorBtn;
    Button toastWarningColorBtn;
    Button toastDefColorBtn;
    Button toastErrorIconBtn;
    Button toastSuccessIconBtn;
    Button toastWarningIconBtn;
    Button toastErrorIconDisableBtn;
    Button toastSuccessIconDisableBtn;
    Button toastWarningIconDisableBtn;
    Button toastErrorIconDisableTintBtn;
    Button toastSuccessIconDisableTintBtn;
    Button toastWarningIconDisableTintBtn;
    Button toastConfigTextBtn;
    Button toastConfigBackgroundBtn;
    Button toastConfigIconSizeBtn;
    Button hintDefaultBtn;
    Button hintCustomBtn;

    Image bottomBtn;
    Image topBtn;
    Text versionTv;
    Text titleTv;
    private final int dur = 10;
    private final String colorOne = "#607d8b";
    private final int width = 24;
    private final int height = 24;
    private final float fontSize = 16f;
    private final float maxFont = 48f;
    private final int midFont = 18;
    private final int minSize = 20;

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get()
            .setStatusBarColor(getColor(ResourceTable.Color_color_primary_dark)); // 设置状态栏颜色
        super.setUIContent(ResourceTable.Layout_content_dynamic_toasts);
        toastDefBtn = (Button) findComponentById(ResourceTable.Id_toast_default);
        toastDefBtn.setClickedListener(this);
        toastDefIconBtn = (Button) findComponentById(ResourceTable.Id_toast_default_icon);
        toastDefIconBtn.setClickedListener(this);
        toastErrorBtn = (Button) findComponentById(ResourceTable.Id_toast_error);
        toastErrorBtn.setClickedListener(this);
        toastSuccessBtn = (Button) findComponentById(ResourceTable.Id_toast_success);
        toastSuccessBtn.setClickedListener(this);
        toastWarningBtn = (Button) findComponentById(ResourceTable.Id_toast_warning);
        toastWarningBtn.setClickedListener(this);
        toastCusBtn = (Button) findComponentById(ResourceTable.Id_toast_custom);
        toastCusBtn.setClickedListener(this);
        toastCusIconBtn = (Button) findComponentById(ResourceTable.Id_toast_custom_icon);
        toastCusIconBtn.setClickedListener(this);
        warningIconBtn = (Button) findComponentById(ResourceTable.Id_toast_warning_icon);
        warningIconBtn.setClickedListener(this);
        warningBtn = (Button) findComponentById(ResourceTable.Id_toast_warning);
        warningBtn.setClickedListener(this);
        toastErrorColorBtn = (Button) findComponentById(ResourceTable.Id_toast_error_color);
        toastErrorColorBtn.setClickedListener(this);
        toastSuccessColorBtn = (Button) findComponentById(ResourceTable.Id_toast_success_color);
        toastSuccessColorBtn.setClickedListener(this);
        toastWarningColorBtn = (Button) findComponentById(ResourceTable.Id_toast_warning_color);
        toastWarningColorBtn.setClickedListener(this);
        toastDefColorBtn = (Button) findComponentById(ResourceTable.Id_toast_default_color);
        toastDefColorBtn.setClickedListener(this);
        initComponent();
    }

    private void initComponent() {
        toastErrorIconBtn = (Button) findComponentById(ResourceTable.Id_toast_error_icon);
        toastErrorIconBtn.setClickedListener(this);
        toastSuccessIconBtn = (Button) findComponentById(ResourceTable.Id_toast_success_icon);
        toastSuccessIconBtn.setClickedListener(this);
        toastWarningIconBtn = (Button) findComponentById(ResourceTable.Id_toast_warning_icon);
        toastWarningIconBtn.setClickedListener(this);
        toastErrorIconDisableBtn = (Button) findComponentById(ResourceTable.Id_toast_error_icon_disable);
        toastErrorIconDisableBtn.setClickedListener(this);
        toastSuccessIconDisableBtn = (Button) findComponentById(ResourceTable.Id_toast_success_icon_disable);
        toastSuccessIconDisableBtn.setClickedListener(this);
        toastWarningIconDisableBtn = (Button) findComponentById(ResourceTable.Id_toast_warning_icon_disable);
        toastWarningIconDisableBtn.setClickedListener(this);
        toastErrorIconDisableTintBtn = (Button) findComponentById(ResourceTable.Id_toast_error_icon_disable_tint);
        toastErrorIconDisableTintBtn.setClickedListener(this);
        toastSuccessIconDisableTintBtn = (Button) findComponentById(ResourceTable.Id_toast_success_icon_disable_tint);
        toastSuccessIconDisableTintBtn.setClickedListener(this);
        toastWarningIconDisableTintBtn = (Button) findComponentById(ResourceTable.Id_toast_warning_icon_disable_tint);
        toastWarningIconDisableTintBtn.setClickedListener(this);
        toastConfigTextBtn = (Button) findComponentById(ResourceTable.Id_toast_config_text);
        toastConfigTextBtn.setClickedListener(this);
        toastConfigBackgroundBtn = (Button) findComponentById(ResourceTable.Id_toast_config_background);
        toastConfigBackgroundBtn.setClickedListener(this);
        toastConfigIconSizeBtn = (Button) findComponentById(ResourceTable.Id_toast_config_icon_size);
        toastConfigIconSizeBtn.setClickedListener(this);
        hintDefaultBtn = (Button) findComponentById(ResourceTable.Id_hint_default);
        hintDefaultBtn.setClickedListener(this);
        hintCustomBtn = (Button) findComponentById(ResourceTable.Id_hint_custom);
        hintCustomBtn.setClickedListener(this);
        versionTv = (Text) findComponentById(ResourceTable.Id_gradle);
        topBtn = (Image) findComponentById(ResourceTable.Id_title_ic);
        topBtn.setClickedListener(this);
        bottomBtn = (Image) findComponentById(ResourceTable.Id_ads_app_bar_shadow);
        bottomBtn.setClickedListener(this);
        titleTv = (Text) findComponentById(ResourceTable.Id_title_tv);
        titleTv.setFont(Font.DEFAULT_BOLD);
    }

    @Override
    public void onClick(Component component) {
        try {
            addComponentClick(component);
            addComponentClick2(component);
            addClickEvent(component);
            addClickEvent2(component);
            addClickEvent3(component);
            addClickEvent4(component);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
    }

    private void addComponentClick(Component component) throws IOException, NotExistException {
        switch (component.getId()) {
            case ResourceTable.Id_toast_default:
                DynamicToast.make(this, getString(ResourceTable.String_without_icon_desc).toString()).show();
                break;
            case ResourceTable.Id_toast_default_icon:
                Element toastIconElement = ElementScatter.getInstance(getContext())
                    .parse(ResourceTable.Graphic_ic_toast_icon);
                DynamicToast.make(this, getString(ResourceTable.String_with_icon_desc), toastIconElement).show();
                break;
            case ResourceTable.Id_toast_error:
                DynamicToast.makeError(this, getString(ResourceTable.String_error_desc)).show();
                break;
            case ResourceTable.Id_toast_success:
                DynamicToast.makeSuccess(
                    this, getString(ResourceTable.String_success_desc)).show();
                break;
            case ResourceTable.Id_toast_warning:
                DynamicToast.makeWarning(getContext(), getString(ResourceTable.String_warning_desc), dur).show();
                break;
            case ResourceTable.Id_toast_custom:
                DynamicToast.make(
                    this, getString(ResourceTable.String_custom_desc),
                    Color.getIntColor("#FFFFFF"), Color.getIntColor("#000000"),
                    dur).show();
                break;
            case ResourceTable.Id_toast_custom_icon:
                Element cusIconElement = ElementScatter.getInstance(getContext())
                    .parse(ResourceTable.Graphic_ic_social_github);
                DynamicToast.make(
                    this, getString(ResourceTable.String_custom_desc),
                    cusIconElement,
                    Color.getIntColor("#FFFFFF"), Color.getIntColor("#000000"),
                    dur).show();
                break;
            default:
                break;
        }
    }

    private void addComponentClick2(Component component) throws IOException, NotExistException {
        switch (component.getId()) {
            case ResourceTable.Id_toast_error_color:
                DynamicToast.Config.getInstance()
                    .setErrorBackgroundColor(Color.getIntColor("#673AB7"))
                    .apply();
                DynamicToast.makeError(this, getString(ResourceTable.String_error_color_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_success_color:
                DynamicToast.Config.getInstance()
                    .setSuccessBackgroundColor(Color.getIntColor("#2196F3"))
                    .apply();
                DynamicToast.makeSuccess(this, getString(ResourceTable.String_success_color_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_warning_color:
                DynamicToast.Config.getInstance()
                    .setWarningBackgroundColor(Color.getIntColor("#8BC34A"))
                    .apply();
                DynamicToast.makeWarning(this, getString(ResourceTable.String_warning_color_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_default_color:
                DynamicToast.Config.getInstance()
                    .setDefaultBackgroundColor(Color.getIntColor(colorOne))
                    .setDefaultTintColor(DynamicColorUtils.getTintColor(
                        Color.getIntColor(colorOne)))
                    .apply();

                DynamicToast.make(
                    this, getString(ResourceTable.String_default_color_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_error_icon:
                Element errorIconElement = ElementScatter.getInstance(getContext())
                    .parse(ResourceTable.Graphic_ic_toast_icon);
                DynamicToast.Config.getInstance()
                    .setErrorIcon(errorIconElement)
                    .apply();
                DynamicToast.makeError(this,
                    getString(ResourceTable.String_error_icon_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            default:
                break;
        }
    }

    private void addClickEvent2(Component component) throws IOException, NotExistException {
        switch (component.getId()) {
            case ResourceTable.Id_toast_warning_icon_disable:
                DynamicToast.Config.getInstance()
                    .setDisableIcon(true)
                    .apply();
                DynamicToast.makeWarning(this,
                    getString(ResourceTable.String_warning_icon_disable_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_error_icon_disable_tint:
                PixelMap pixelMap = ImageTool.getPixelMap(getContext(),
                    ResourceTable.Media_ic_launcher, width, height).get();
                PixelMapElement disableTintElement = new PixelMapElement(pixelMap);
                DynamicToast.Config.getInstance()
                    .setErrorIcon(disableTintElement)
                    .setTintIcon(false)
                    .apply();
                DynamicToast.makeError(this,
                    getString(ResourceTable.String_error_icon_disable_tint_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_success_icon_disable_tint:
                PixelMap mPixelMap = ImageTool.getPixelMap(getContext(),
                    ResourceTable.Media_ic_launcher, width, height).get();
                PixelMapElement successTintElement = new PixelMapElement(mPixelMap);
                DynamicToast.Config.getInstance()
                    .setSuccessIcon(successTintElement)
                    .setTintIcon(false)
                    .apply();
                DynamicToast.makeSuccess(this,
                    getString(ResourceTable.String_success_icon_disable_tint_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_warning_icon_disable_tint:
                PixelMap pMap = ImageTool.getPixelMap(getContext(),
                    ResourceTable.Media_ic_launcher, width, height).get();
                PixelMapElement waringTintElement = new PixelMapElement(pMap);
                DynamicToast.Config.getInstance()
                    .setWarningIcon(waringTintElement)
                    .setTintIcon(false)
                    .apply();
                DynamicToast.makeWarning(this,
                    getString(ResourceTable.String_warning_icon_disable_tint_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            default:
                break;
        }
    }

    private void addClickEvent4(Component component) throws IOException, NotExistException {
        switch (component.getId()) {
            case ResourceTable.Id_toast_success_icon:
                Element successIconElement = ElementScatter.getInstance(getContext())
                    .parse(ResourceTable.Graphic_ic_toast_icon);
                DynamicToast.Config.getInstance()
                    .setSuccessIcon(successIconElement)
                    .apply();
                DynamicToast.makeSuccess(this,
                    getString(ResourceTable.String_success_icon_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_warning_icon:
                Element warningIconElement = ElementScatter.getInstance(getContext())
                    .parse(ResourceTable.Graphic_ic_toast_icon);
                DynamicToast.Config.getInstance()
                    .setWarningIcon(warningIconElement)
                    .apply();
                DynamicToast.makeWarning(this,
                    getString(ResourceTable.String_warning_icon_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_error_icon_disable:
                DynamicToast.Config.getInstance()
                    .setDisableIcon(true)
                    .apply();
                DynamicToast.makeError(this,
                    getString(ResourceTable.String_error_icon_disable_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_success_icon_disable:
                DynamicToast.Config.getInstance()
                    .setDisableIcon(true)
                    .apply();
                DynamicToast.makeSuccess(this,
                    getString(ResourceTable.String_success_icon_disable_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            default:
                break;
        }
    }

    private void addClickEvent(Component component) throws IOException, NotExistException {
        switch (component.getId()) {
            case ResourceTable.Id_hint_default:
                DynamicHint.show(hintDefaultBtn,
                    DynamicHint.make(getAbility(), getString(ResourceTable.String_default_hint)));
                break;
            case ResourceTable.Id_hint_custom:
                DynamicHint.Config.getInstance()
                    .setDefaultBackgroundColor(Color.getIntColor(colorOne))
                    .apply();
                PixelMap pixelMap = ImageTool.getPixelMap(getContext(),
                    ResourceTable.Media_adt_ic_warning, minSize, minSize).get();
                PixelMapElement hintElement = new PixelMapElement(pixelMap);

                DynamicHint.show(hintCustomBtn, DynamicHint.make(getContext(),
                    getString(ResourceTable.String_custom_hint), hintElement));
                DynamicHint.Config.getInstance().reset();
                break;
            case ResourceTable.Id_ads_app_bar_shadow:
                Intent intents = new Intent();
                Operation operation = new Intent.OperationBuilder()
                    .withUri(Uri.parse(getContext().getString(ResourceTable.String_urlGithub)))
                    .withAction(IntentConstants.ACTION_SEARCH).build();
                intents.setOperation(operation);
                startAbility(intents);
                break;
            case ResourceTable.Id_title_ic:
                CustomDialog customDialog = new CustomDialog(getContext());
                customDialog.show();
                break;
            default:
                break;
        }
    }

    private void addClickEvent3(Component component) throws IOException, NotExistException {
        switch (component.getId()) {
            case ResourceTable.Id_toast_config_text:
                Font font = new Font.Builder(Font.SERIF.getName()).makeItalic(true).build();
                Element configTextElement = ElementScatter.getInstance(getContext())
                    .parse(ResourceTable.Graphic_ic_toast_icon);
                DynamicToast.Config.getInstance()
                    .setErrorIcon(configTextElement)
                    .setTextSize(midFont)
                    .setTextTypeface(font)
                    .setErrorBackgroundColor(Color.getIntColor("#2196F3"))
                    .apply();
                DynamicToast.makeError(this, getString(ResourceTable.String_text_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_config_background:
                Element configBackgroundElement = ElementScatter.getInstance(getContext())
                    .parse(ResourceTable.Graphic_bg_custom_toast);
                DynamicToast.Config.getInstance()
                    .setToastBackground(configBackgroundElement)
                    .setIconSize(DynamicUnitUtils.convertDpToPixels(getContext(), fontSize))
                    .apply();
                DynamicToast.makeSuccess(this, getString(ResourceTable.String_background_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            case ResourceTable.Id_toast_config_icon_size:
                DynamicToast.Config.getInstance()
                    .setIconSize(DynamicUnitUtils.convertDpToPixels(getContext(), maxFont))
                    .apply();
                DynamicToast.makeWarning(this, getString(ResourceTable.String_icon_size_desc)).show();
                DynamicToast.Config.getInstance().reset();
                break;
            default:
                break;
        }
    }

    @Override
    public void onActive() {
        super.onActive();
        versionTv.setText(String.format(getString(ResourceTable.String_format_version),
            getString(ResourceTable.String_library_version)));
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
